const { request } = require('../../utils/api');

Page({
  data: {
    username: '',
    password: '',
    nickname: ''
  },

  onUsernameInput: function(e) {
    this.setData({
      username: e.detail.value
    });
  },

  onPasswordInput: function(e) {
    this.setData({
      password: e.detail.value
    });
  },

  onNicknameInput: function(e) {
    this.setData({
      nickname: e.detail.value
    });
  },

  onRegister: function() {
    if (!this.data.username || !this.data.password) {
      wx.showToast({
        title: '用户名和密码不能为空',
        icon: 'none'
      });
      return;
    }

    wx.showLoading({
      title: '注册中...',
      mask: true
    });

    request('/api/register', {
      method: 'POST',
      data: {
        username: this.data.username,
        password: this.data.password,
        nickname: this.data.nickname
      }
    }).then(response => {
      wx.hideLoading();
      if (response.success) {
        wx.showToast({
          title: '注册成功',
          icon: 'success',
          duration: 1500,
          complete: () => {
            setTimeout(() => {
              wx.navigateBack();
            }, 1500);
          }
        });
      } else {
        wx.showToast({
          title: response.message || '注册失败',
          icon: 'none'
        });
      }
    }).catch(error => {
      wx.hideLoading();
      console.error('注册失败:', error);
      wx.showToast({
        title: error.message || '注册失败，请重试',
        icon: 'none'
      });
    });
  }
}); 